<?php
/**
 * Created by PhpStorm.
 * User: Egg
 * Date: 2018/4/9
 * Time: 14:34
 */

namespace Admin\Model;


use Think\Exception;
use Wechat\Model\WxLogic;

class ImageReplyLogic
{

    /**
     * 获取media_id
     * @param $wx_mid
     * @param $type
     * @param $file_path
     * @return string
     * @internal param $string
     */
    public function get_media_id($wx_mid, $type, $file_path)
    {
        $result = WxLogic::get_media_id('image', $file_path);
        $where['id'] = $wx_mid;
        $where['media_id'] = $result['media_id'];
        $where['tag'] = $result['url'];
        $where['type'] = $type;
        try {
            D('Attachment')
                ->save($where);
            return $where['media_id'];
        } catch (Exception $exception) {
            return '';
        }
    }

    public function add_image_reply_rule()
    {
        //开启事务
        $trans = M();
        $trans->startTrans();
        try {
            $rule_name = I('rule_name');
            $key_words = I('rule_keywords');
            $wx_mid = I('wx_mid');
            if (empty($rule_name) || empty($key_words)) {
                \Common\Common\print_json_data(201, '', '请填写完全后提交！');
                exit;
            }
            if ($wx_mid <= 0) {
                \Common\Common\print_json_data(201, '', '请选择图片！');
                exit;
            }
            $file_path = D('attachment')
                ->getById($wx_mid);
            chdir('Uploads');
            $file_path = '@' . getcwd() . $file_path['attachment'];

//            $media_id = $this->get_media_id($wx_mid, 'image', $file_path);
            $media_id = 1;
            if (empty($media_id)) {
                throw new Exception();
            }
            $img_rule_data['title'] = $rule_name;
            $img_rule_data['description'] = I('content');
            $img_rule_data['mediaid'] = $media_id;
            $img_rule_data['url'] = I('url');
            $img_rule_data['createtime'] = time();
            $img_rule_id = M('wx_images_reply')
                ->add($img_rule_data);

            $key_words = explode(',', $key_words);
            foreach ($key_words as $key_word) {
                $reply_rule_data['rule_name'] = $rule_name;
                $reply_rule_data['rule_keywords'] = $key_word;
                $reply_rule_data['moudle'] = 'image';
                $reply_rule_data['rule_id'] = $img_rule_id;
                $reply_rule_data['rule_status'] = 'ACTIVE';
                $reply_rule_data['createtime'] = time();
                $reply_rule_data['createuid'] = session('uid');
                $rule_replay_id = M('wx_reply_rule')
                    ->add($reply_rule_data);
                if (empty($img_rule_id) || empty($rule_replay_id)) {
                    throw new Exception();
                }
            }
        } catch (Exception $e) {
            $trans->rollback();
            echo $e->getMessage();
            \Common\Common\print_json_data(201, '', '储存失败');
            exit;
        }
        $trans->commit();
        \Common\Common\print_json_data(200, '', '添加成功');
    }

    /**
     * 更新图片回复信息
     */
    public function update_image_reply_rule()
    {
        //开启事务
        $trans = M();
        $trans->startTrans();
        try {
            $rid = I('rid');
            $rule_name = I('rule_name');
            $key_words = I('rule_keywords');
            $url = I('url');

            $wx_mid = I('wx_mid');
            if (empty($rule_name) || empty($key_words)) {
                \Common\Common\print_json_data(201, '', '请填写完全后提交！');
                exit;
            }
            if ($wx_mid >= 0) {
                //获取media_id
                $file_path = D('attachment')
                    ->getById($wx_mid);
                chdir('Uploads');
                $file_path = '@' . getcwd() . $file_path['attachment'];
//            $media_id = $this->get_media_id($wx_mid, 'image', $file_path);
                $media_id = 1;
                if (empty($media_id)) {
                    throw new Exception();
                }
                $image_where['mediaid'] = $media_id;

            }


            $db_rule = M('wx_reply_rule');
            //更新到 wx_reply_rule 数据库内
//            $rule_id = $db_rule->getById($rid);
//            $rule_id = $rule_id['rule_id'];
//            $rule_where['id'] = $rid;
//            $rule_where['rule_name'] = $rule_name;
//            $rule_where['rule_keywords'] = $key_words;
//            $rule_where['createtime'] = time();
//            $rule_where['createuid'] = session('uid');
//            $db_rule->save($rule_where);
            $rule_where['rule_id'] = $rid;
            $db_rule->where($rule_where)->delete();

            $rule_where['rule_name'] = $rule_name;
            $rule_where['createtime'] = time();
            $rule_where['moudle'] = 'image';
            $rule_where['createuid'] = session('uid');
            $key_words = explode(',', $key_words);
            foreach ($key_words as $key_word) {
                $rule_where['rule_keywords'] = $key_word;
                $db_rule->add($rule_where);
            }


            //更新到 wx_images_reply
            $db_image = M('wx_images_reply');
            $image_where['rule_id'] = $rid;
            $image_where['title'] = $rule_name;
            $image_where['description'] = I('content');
            $image_where['url'] = $url;
            $image_where['createtime'] = time();
            $db_image->save($image_where);

        } catch (Exception $e) {
            $trans->rollback();
            echo $e->getMessage();
            \Common\Common\print_json_data(201, '', '储存失败');
            exit;
        }
        $trans->commit();
        \Common\Common\print_json_data(200, '', '更新成功');
    }
}